%Invoke mtest's runtests() for all of the Snow Dots directories
%
%   didPass = runAllDotsTests(varargin)
%
%   @param varargin optional field-value pairs for setting arbitrary
%   globally avalable testing data.
%   @details
%   Fields and values contained in @a varargin will be applied to the
%   global struct DOTS_TEST_DATA.  Test suites may expect or may wish to
%   wish to access this data.  For example, the dotsTestCase superclass for
%   Snow Dots test can use @a varargin and DOTS_TEST_DATA to set the
%   global state of singleton classes before running each test.
%   @details
%   Run all unit test for the Snow Dots code.  Returns true if all
%   tests pass.  If any test fails, aborts and returns false.
%   @ingroup utilities

function didPass = runAllDotsTests(varargin)

clear global DOTS_TEST_DATA
global DOTS_TEST_DATA

if nargin
    DOTS_TEST_DATA = struct;
    for ii = 1:2:nargin
        param = varargin{ii};
        val = varargin{ii+1};
        DOTS_TEST_DATA.(param) = val;
    end
end

% locate the dots code tree
[p, f] = fileparts(mfilename('fullpath'));
dotsRoot = fullfile(p, '..');

initialDir = pwd;

disp(sprintf('RUNNING ALL TESTS FOR SNOW DOTS'));
tic;
didPass = runTestsInDir(dotsRoot);
cd(initialDir)

if didPass
    disp(sprintf('\nSNOW DOTS PASSED ALL TESTS in %f seconds', toc));
end

function didPass = runTestsInDir(d)
cd(d);
absPath = pwd;
directoryList = dir(absPath);

disp(sprintf('\n\n%s', absPath));
didPass = runtests(absPath);

if didPass
    for ii = 1:length(directoryList)
        if directoryList(ii).isdir && isempty(regexp(directoryList(ii).name, '^\.'))
            % recursive: into subdirectories (ignore ".svn", etc)
            fullPath = fullfile(absPath, directoryList(ii).name);
            didPass = runTestsInDir(fullPath);
            if ~didPass
                break
            end
        end
    end
end